package vip.xiaonuo.hontu.issued.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Objects;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.pojo.httpclient.ReturnJson;
import vip.xiaonuo.facedoor.iservice.IVisitorDealWithService;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParam;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParamRet;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.deviceset.entity.DwDeviceSet;
import vip.xiaonuo.facedoor.modular.deviceset.service.DwDeviceSetService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.service.VisitorDeviceService;
import vip.xiaonuo.facedoor.rule.HTRulesEngine;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.facedoor.vo.dewo.AddPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.DeletePersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.UpdatePersonRequest;

@Service(value = RuleConstant.HT + RuleConstant.VISITOR_DEAL_WITH)
public class HTVisitorDealWithServiceImpl implements IVisitorDealWithService {


    @Autowired
    private VisitorDeviceService visitorDeviceService;

    @Autowired
    private DeviceLogService logService;

    @Autowired
    private VisitorService visitorService;

    @Autowired
    private DwDeviceSetService dwDeviceSetService;


    @Override
    public PersonBaseParamRet getBaseParam(PersonBaseParam personBasePerson) {
        return new PersonBaseParamRet().setUrl("http://" + personBasePerson.getDeviceBase().getCabinetIp() + ":" + personBasePerson.getDeviceBase().getCabinetPort());
    }

    @Override
    public void deleteException(VisitorDevice visitorDevice, Visitor visitor, DeviceBase deviceBase, Exception e) {
        visitorDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        visitorDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
        visitorDeviceService.updateById(visitorDevice);
        logService.addVisitorLog(visitor.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, UPDATE);
    }

    @Override
    public void delete(VisitorDevice visitorDevice, Visitor visitor, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
        DwDeviceSet deviceSet = dwDeviceSetService.getOne(new LambdaQueryWrapper<DwDeviceSet>().eq(DwDeviceSet::getDeviceBaseId, deviceBase.getId()));
        if (deviceSet == null) {
            throw new ServiceException(500, "设置尚未进行配置！");
        }
        DeletePersonRequest deletePersonRequest = new DeletePersonRequest();
        deletePersonRequest.setIp(deviceBase.getCabinetIp());
        deletePersonRequest.setPort(deviceBase.getCabinetPort());
        deletePersonRequest.setPersonnelId("2" + visitor.getId());
        ReturnJson returnJson = HTRulesEngine.faceDoorDtoService.deletePerson(null, deletePersonRequest);
        visitorDeviceService.removeById(visitorDevice.getId());
        long count = visitorDeviceService.count(new LambdaQueryWrapper<VisitorDevice>().eq(VisitorDevice::getVisitorId, visitor.getId()));
        if (count == 0 && Objects.equals(visitor.getDelFlag(), "1")) {
            visitorService.removeById(visitorDevice.getVisitorId());
        }
        logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, DELETE);
    }

    @Override
    public void add(VisitorDevice visitorDevice, Visitor visitor, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
        DwDeviceSet deviceSet = dwDeviceSetService.getOne(new LambdaQueryWrapper<DwDeviceSet>().eq(DwDeviceSet::getDeviceBaseId, deviceBase.getId()));
        if (deviceSet == null) {
            throw new ServiceException(500, "设置尚未进行配置！");
        }

        AddPersonRequest addPersonRequest = new AddPersonRequest();
        addPersonRequest.setIp(deviceBase.getCabinetIp());
        addPersonRequest.setPort(deviceBase.getCabinetPort());
        addPersonRequest.setPersonType(1);
        addPersonRequest.setName(visitor.getName());
        addPersonRequest.setFaceFeatureImgPath(visitor.getPhoto());
        // todo 待配置
        //addPersonRequest.setOrgId()
        addPersonRequest.setPersonnelId("2" + visitor.getId());
        ReturnJson returnJson = HTRulesEngine.faceDoorDtoService.addPerson(null, addPersonRequest);
        logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, ADD);
        visitorDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
        visitorDeviceService.updateById(visitorDevice);
    }

    @Override
    public void addException(VisitorDevice visitorDevice, Visitor visitor, DeviceBase deviceBase, Exception e) {
        visitorDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        visitorDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
        visitorDeviceService.updateById(visitorDevice);
        logService.addVisitorLog(visitor.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, ADD);
    }

    @Override
    public void updateException(VisitorDevice visitorDevice, Visitor visitor, DeviceBase deviceBase, Exception e) {
        visitorDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
        visitorDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
        visitorDeviceService.updateById(visitorDevice);
        logService.addVisitorLog(visitor.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, UPDATE);
    }

    @Override
    public void update(VisitorDevice visitorDevice, Visitor visitor, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
        DwDeviceSet deviceSet = dwDeviceSetService.getOne(new LambdaQueryWrapper<DwDeviceSet>().eq(DwDeviceSet::getDeviceBaseId, deviceBase.getId()));
        if (deviceSet == null) {
            throw new ServiceException(500, "设置尚未进行配置！");
        }
        UpdatePersonRequest addPersonRequest = new UpdatePersonRequest();
        addPersonRequest.setIp(deviceBase.getCabinetIp());
        addPersonRequest.setPort(deviceBase.getCabinetPort());
        addPersonRequest.setPersonType(1);
        addPersonRequest.setName(visitor.getName());
        addPersonRequest.setFaceFeatureImgPath(visitor.getPhoto());
        addPersonRequest.setPersonnelId("2" + visitor.getId());
        ReturnJson returnJson = HTRulesEngine.faceDoorDtoService.updatePerson(null, addPersonRequest);
        visitorDevice.setStatus(MemberDeviceConstant.SYNC_SUCCESS);
        visitorDeviceService.updateById(visitorDevice);
        logService.addVisitorLog(visitor.getName(), deviceBase.getName(), JSON.toJSONString(returnJson), SUCCESS, ADD);

    }

}
